package com.app.megaprosoft.modyimcarcare;
 
import java.util.HashMap;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
 
public class Carcare_SessionManagement {
    // Shared Preferences
    SharedPreferences pref;
     
    // Editor for Shared preferences
    Editor editor;
     
    // Context
    Context _context;
     
    // Shared pref mode
    int PRIVATE_MODE = 0;
     
    // Sharedpref file name
    private static final String PREF_NAME = "AndroidHivePref";
    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";
    
    public static final String KEY_FFUserOID = "FFUserOID";
    public static final String KEY_UserName = "UserName";
    public static final String KEY_Password = "Password";
    public static final String KEY_DepCode = "DepCode";
    public static final String KEY_DepName = "DepName";
    public static final String KEY_UserGroupOID = "UserGroupOID";
    public static final String KEY_UserGroupName = "UserGroupName";
    public static final String KEY_UserGroupCode = "UserGroupCode";
    public static final String KEY_PicPath = "PicPath";
    public static final String KEY_TabletCode = "TabletCode";
    public static final String KEY_TabletName = "TabletName";
    
    public static final String KEY_MODE = "MODE";
    
    // Constructor
    public Carcare_SessionManagement(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
     
    /**
     * Create login session
     * */
    public void createLoginSession( String FFUserOID, 
    								String UserName,
    								String Password,
    								String DepCode,
    								String DepName,
    								String UserGroupOID,
    								String UserGroupName,
    								String UserGroupCode,
    								String PicPath,
    								String TabletCode,
    								String TabletName,
    								String MODE){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);
         
        editor.putString(KEY_FFUserOID, FFUserOID);
        editor.putString(KEY_UserName, UserName);
        editor.putString(KEY_Password, Password);
        editor.putString(KEY_DepCode, DepCode);
        editor.putString(KEY_DepName, DepName);
        editor.putString(KEY_UserGroupOID, UserGroupOID);
        editor.putString(KEY_UserGroupName, UserGroupName);
        editor.putString(KEY_UserGroupCode, UserGroupCode);
        editor.putString(KEY_PicPath, PicPath);
        editor.putString(KEY_TabletCode, TabletCode);
        editor.putString(KEY_TabletName, TabletName);
        editor.putString(KEY_MODE, MODE);
        
         
        // commit changes
        editor.commit();
    } 

    public void ModeSession(String MODE){
    	// Storing login value as TRUE
    	editor.putString(KEY_MODE, MODE);
    	// commit changes
    	editor.commit();
    } 
    
     
    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, Carcare_Login.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             
            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             
            // Staring Login Activity
            _context.startActivity(i);
        }
         
    }
    /**
     * Get stored session data
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();
        
        // user name
        
        user.put(KEY_FFUserOID, pref.getString(KEY_FFUserOID, null));
        user.put(KEY_UserName, pref.getString(KEY_UserName, null));
        user.put(KEY_Password, pref.getString(KEY_Password, null));
        user.put(KEY_DepCode, pref.getString(KEY_DepCode, null));
        user.put(KEY_DepName, pref.getString(KEY_DepName, null));
        user.put(KEY_UserGroupOID, pref.getString(KEY_UserGroupOID, null));
        user.put(KEY_UserGroupName, pref.getString(KEY_UserGroupName, null));
        user.put(KEY_UserGroupCode, pref.getString(KEY_UserGroupCode, null));
        user.put(KEY_PicPath, pref.getString(KEY_PicPath, null));
        user.put(KEY_TabletCode, pref.getString(KEY_TabletCode, null));
        user.put(KEY_TabletName, pref.getString(KEY_TabletName, null));
        user.put(KEY_MODE, pref.getString(KEY_MODE, null));
         
        // return user
        return user;
    }
     
    /**
     * Clear session details
     * */
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();
         
        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, Carcare_Login.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         
        // Staring Login Activity
        _context.startActivity(i);
    }
     
    /**
     * Quick check for login
     * **/
    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}